package bjfuwjx.exp4;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

public class RedFlag extends JFrame{
    public static CountingTimeThread TimeThread=new CountingTimeThread();
    public static long ProgramStartTime=System.currentTimeMillis();
    public static long PauseStartTime=ProgramStartTime;
    public static long PauseCount=0;
    public static String INITIAL_TIME_TEXT="0";
    public static CountingCountThread CountThread=new CountingCountThread();
    public static int CountSum=0;
    public static int CountNum=0;
    public static String INITIAL_COUNT_TEXT=CountSum+"/"+CountNum;
    public static double speed=0.2;
    public static JLabel init_time_text=new JLabel(INITIAL_TIME_TEXT);
    public static JLabel init_count_text=new JLabel(INITIAL_COUNT_TEXT);
    public static JButton bt1=new JButton("开始");
    public static JButton bt2=new JButton("加速");
    public static JButton bt3=new JButton("拦截");
    public static JButton bt4=new JButton("减速");
    public static JButton bt5=new JButton("停止");
    public static int cnt=0;
    public static int click=0;
    public static CopyOnWriteArrayList<Ball> list1 = new CopyOnWriteArrayList<>();
    public static CopyOnWriteArrayList<Ball> list2 = new CopyOnWriteArrayList<>();
    public static CopyOnWriteArrayList<ShootingBall> list3=new CopyOnWriteArrayList<>();
    public void Show() {
        this.setTitle("红旗-9----北林计算机王思进开发");
        this.setSize(new Dimension(1000,800));
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(100,100);
        this.setLayout(new BorderLayout());
        Container buttons=new Container();
        buttons.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
        Dimension d1=new Dimension(100,25);
        Dimension d2=new Dimension(250,25);
        Dimension d3=new Dimension(50,25);
        JTextField Time=new JTextField("计时(秒):");
        Time.setEditable(false);
        JPanel TimerCount= new JPanel();
        TimerCount.add(init_time_text);
        TimerCount.setPreferredSize(d3);
        TimerCount.setBackground(Color.WHITE);
        bt1.setPreferredSize(d1);
        bt2.setPreferredSize(d1);
        bt2.setEnabled(false);
        bt3.setPreferredSize(d2);
        bt3.setEnabled(false);
        bt4.setPreferredSize(d1);
        bt4.setEnabled(false);
        bt5.setPreferredSize(d1);
        bt5.setEnabled(false);
        JTextField Counter=new JTextField("计数(次):");
        Counter.setEditable(false);
        JPanel Count= new JPanel();
        Count.add(init_count_text);
        Count.setPreferredSize(d3);
        Count.setBackground(Color.WHITE);
        buttons.add(Time);
        buttons.add(TimerCount);
        buttons.add(bt1);
        buttons.add(bt2);
        buttons.add(bt3);
        buttons.add(bt4);
        buttons.add(bt5);
        buttons.add(Counter);
        buttons.add(Count);
        this.add(buttons,BorderLayout.PAGE_END);
        this.setVisible(true);
    }
    public static Double[] setPosition() {
        Double[] position=new Double[2];
        position[0]=450.0;
        position[1]=750.0;
        return position;
    }
    public static void main(String[] args) {
        RedFlag rf=new RedFlag();
        rf.Show();
        MyPanel Draw=new MyPanel(list1,list2,list3);
        Draw.setBackground(Color.WHITE);
        rf.add(Draw);
        BallThread ballthread=new BallThread(list1,list2,Draw);
        BallMove move_thread=new BallMove(list1,list2,Draw);
        ArrayList<ShootingBallThread> thread=new ArrayList<>();
        final int[] f1 = {0};
        bt1.addActionListener(e -> {
            if (f1[0]==0) {
                TimeThread.start();
                ballthread.start();
                move_thread.start();
                f1[0]++;
            }
            if (TimeThread.IsStop) {
                PauseCount+=(System.currentTimeMillis()-PauseStartTime);
                TimeThread.IsStop=false;
                CountThread.IsStop=false;
                ballthread.IsStop=false;
                move_thread.IsStop=false;
                for (ShootingBallThread t:thread){
                    t.IsStop=false;
                }
                bt1.setEnabled(false);
                bt2.setEnabled(true);
                bt3.setEnabled(true);
                bt4.setEnabled(true);
                bt5.setEnabled(true);
                if (speed==1) {
                    bt4.setEnabled(false);
                }
                if (speed==16) {
                    bt2.setEnabled(false);
                }
            }
        });
        final int[] f2 = {0};
        bt2.addActionListener(e -> {
            speed+=0.3;
            f2[0]++;
            if (f2[0]>0) {
                bt4.setEnabled(true);
            }
            if (f2[0]==4) {
                bt2.setEnabled(false);
            }
        });
        final int[] f3 = {0};
        bt3.addActionListener(e -> {
            if (f3[0]==0) {
                CountThread.start();
                f3[0]++;
            }
            click++;
            ShootingBall ball=new ShootingBall(setPosition());
            list3.add(ball);
            Draw.add3(ball);
            ShootingBallThread t=new ShootingBallThread(ball,Draw,list1,list2);
            thread.add(t);
            if (click-cnt<=10){
                t.start();
                t.IsStop=false;
                CountSum+=1;
            }
            init_count_text.setText(CountThread.format(CountNum,CountSum));
            if (click-cnt>10){
                PauseStartTime=System.currentTimeMillis();
                TimeThread.IsStop=true;
                CountThread.IsStop=true;
                ballthread.IsStop=true;
                move_thread.IsStop=true;
                for (ShootingBallThread tt:thread){
                    tt.IsStop=true;
                }
                bt5.setEnabled(false);
                bt4.setEnabled(false);
                bt3.setEnabled(false);
                bt2.setEnabled(false);
                bt1.setEnabled(true);
                JOptionPane.showMessageDialog(null,"请点击开始继续游戏","Error",JOptionPane.ERROR_MESSAGE);
            }
        });
        bt4.addActionListener(e -> {
            f2[0]--;
            speed-=0.3;
            if (f2[0]==0) {
                bt4.setEnabled(false);
            }
            if (f2[0]<4) {
                bt2.setEnabled(true);
            }
        });
        bt5.addActionListener(e -> {
            if (!TimeThread.IsStop) {
                PauseStartTime=System.currentTimeMillis();
                TimeThread.IsStop=true;
                CountThread.IsStop=true;
                ballthread.IsStop=true;
                move_thread.IsStop=true;
                for (ShootingBallThread t:thread){
                    t.IsStop=true;
                }
                bt5.setEnabled(false);
                bt4.setEnabled(false);
                bt3.setEnabled(false);
                bt2.setEnabled(false);
                bt1.setEnabled(true);
            }
        });
    }
}
